using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XFGameFramework;
using XFFSM;
using XFABManager;
using System;

public class GameController : Controller
{

    GameSaveController gameSaveController => Module.LoadController<GameSaveController>();
    private GameModel gameModel;

    public void usingsetting()
    {

        AudioController music = AudioPlayer.GetAuidoController("Music");
        AudioController sound = AudioPlayer.GetAuidoController("Sound");
        Debug.Log("screenindex:" + gameModel.screenindex);
        music.Volume = MusicVolume;
        sound.Volume = SoundVolume;
        

    } 
    public override void OnInit()
    {
        base.OnInit();
        gameModel = gameSaveController.GetGlobalSave();
    }
    public void Reload()
    {
        gameModel = gameSaveController.GetGlobalSave();
    }
    public GameModel GetGameModel()
    {
        // GameModel model = Module.GetModel<GameModel>();
        // if (model == null)
        // {
        //     model = new GameModel();
        //     Module.AddModel(model);
        // }
        // return model;
        gameModel = gameSaveController.GetGlobalSave();
        return gameModel;
    }
    public int GetCurrentArchiveIndex()
    {
        return gameModel.currentArchiveIndex;
    }
    public void SetArchiveIndex(int index)
    {   
        
        gameModel.currentArchiveIndex = index;
        gameSaveController.SaveGlobalSave();
    }
    public void SetGameState(GameState gameState)
    {
        gameModel.gameState = gameState;

        //更新状态机
        FSMController controller = FSMController.GetFSM("GameState");
        if (controller == null)
        {
            //加载并启动状态机
            RuntimeFSMController fsm = AssetBundleManager.LoadAsset<RuntimeFSMController>(Module.ProjectName, "GameState");
            controller = FSMController.StartupFSM("GameState", fsm, Module);
        }

        controller.SetInt("State", (int)gameState);
    }

    ///所有属性的get与set方法
    public int width
    {
        get { return gameModel.width; }
        set { gameModel.width = value;gameSaveController.SaveGlobalSave();}
    }
    public int height
    {
        get { return gameModel.height; }
        set { gameModel.height = value;gameSaveController.SaveGlobalSave(); }
    }
    public float MusicVolume
    {
        get { return gameModel.MusicVolume; }
        set {gameModel.MusicVolume = value; gameSaveController.SaveGlobalSave();}
    }
    public float SoundVolume
    {
        get { return gameModel.SoundVolume; }
        set { gameModel.SoundVolume = value; gameSaveController.SaveGlobalSave();}
    }
    public bool iswindow
    {
        get { return gameModel.iswindow; }
        set { gameModel.iswindow = value; gameSaveController.SaveGlobalSave();}
    }
    public int currentArchiveIndex
    {
        get { return gameModel.currentArchiveIndex; }    
        set { gameModel.currentArchiveIndex = value;gameSaveController.SaveGlobalSave(); }
    }

    public int screenindex
    {
        get { return gameModel.screenindex; }
        set { gameModel.screenindex = value;gameSaveController.SaveGlobalSave(); }
    }

}
